/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.coupon.controller;

import cn.icanci.basic.commonutils.R;
import cn.icanci.coupon.client.TradeLogService;
import cn.icanci.coupon.form.BeCouponForm;
import cn.icanci.coupon.form.CouponVO;
import cn.icanci.coupon.form.OptionModule;
import cn.icanci.coupon.form.ViewVO;
import cn.icanci.coupon.model.Coupon;
import cn.icanci.coupon.service.CouponService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import com.google.common.collect.Lists;

/**
 * @author icanci
 * @date Created in 2021/05/01 09:39
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Resource
    private CouponService   couponService;
    @Resource
    private TradeLogService tradeLogService;

    @PostMapping("/addCoupons")
    public R addCoupons(@RequestBody BeCouponForm beCouponForm) {
        try {
            tradeLogService.setLogInfo("COUPON", "ADD_COUPONS", "");
            couponService.addCoupons(beCouponForm);
            return R.ok().message("优惠券保存成功");
        } catch (Exception e) {
            return R.error().message(e.getMessage());
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/searchAllCoupons")
    public R searchAllCoupons() {
        try {
            tradeLogService.setLogInfo("COUPON", "SEARCH_ALL_COUPONS", "");
            List<CouponVO> couponList = couponService.searchAllCoupons();
            return R.ok().message("优惠券查询成功").data("couponList", couponList);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/searchAllCouponAnalysis")
    public R searchAllCouponAnalysis() {
        try {
            tradeLogService.setLogInfo("COUPON", "SEARCH_ALL_COUPONS", "");
            List<CouponVO> couponList = couponService.searchAllCoupons();
            List<ViewVO> viewVOS = Lists.newArrayList();

            Map<String, List<CouponVO>> collect = couponList.stream().collect(Collectors.groupingBy(o -> o.getStartCity() + "-" + o.getEndCity()));
            for (Map.Entry<String, List<CouponVO>> entry : collect.entrySet()) {
                ViewVO viewVO = new ViewVO();
                viewVO.setType(entry.getKey());
                viewVO.setValue(entry.getValue().size() * 1.0 / couponList.size());
                viewVOS.add(viewVO);
            }
            return R.ok().message("优惠券查询成功").data("couponAnalysis", viewVOS);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/searchAllCouponsLimitTime")
    public R searchAllCouponsLimitTime() {
        try {
            tradeLogService.setLogInfo("COUPON", "SEARCH_ALL_COUPONS_LIMIT_TIME", "");
            List<CouponVO> couponList = couponService.searchAllCouponsLimitTime();
            return R.ok().message("优惠券查询成功").data("couponList", couponList);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/searchAllCouponsByCondition/{condition}")
    public R searchAllCouponsByCondition(@PathVariable("condition") String condition) {
        try {
            tradeLogService.setLogInfo("COUPON", "SEARCH_ALL_COUPONS_BY_CONDITION", "");
            List<CouponVO> couponList = couponService.searchAllCouponsByCondition(condition);
            return R.ok().message("优惠券查询成功").data("couponList", couponList);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/searchCouponListUNK")
    public R searchCouponListUNK() {
        try {
            tradeLogService.setLogInfo("COUPON", "SEARCH_COUPON_LIST_UNK", "");
            List<OptionModule> couponList = couponService.searchCouponListUNK();
            return R.ok().message("优惠券查询成功").data("couponUNKList", couponList);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/searchOneCouponListByUNK/{unk}")
    public R searchOneCouponListByUNK(@PathVariable("unk") String unk) {
        try {
            tradeLogService.setLogInfo("COUPON", "SEARCH_ONE_COUPON_LIST_BY_UNK", "");
            List<Coupon> couponList = couponService.searchOneCouponListByUNK(unk);
            return R.ok().message("查询成功").data("couponUNKList", couponList);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/insertOneCoupon/{uniqueKey}/{userId}")
    public R insertOneCoupon(@PathVariable("uniqueKey") String uniqueKey, @PathVariable("userId") Integer userId) {
        try {
            tradeLogService.setLogInfo("COUPON", "INSERT_ONE_COUPON", "");
            tradeLogService.info(log, "CouponController insertOneCoupon,uniqueKey:{},userId:{} ", uniqueKey, userId);
            return couponService.insertOneCoupon(uniqueKey, userId);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/updateOneCoupon/{couponNumber}/{userId}")
    public R updateOneCoupon(@PathVariable("couponNumber") String couponNumber, @PathVariable("userId") Integer userId) {
        try {
            tradeLogService.setLogInfo("COUPON", "UPDATE_ONE_COUPON", "");
            tradeLogService.info(log, "CouponController updateOneCoupon,couponNumber:{},userId:{} ", couponNumber, userId);
            return couponService.updateOneCoupon(couponNumber, userId);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/queryUserOneCoupon/{couponNumber}/{userId}")
    public R queryOneCoupon(@PathVariable("userId") Integer userId,@PathVariable("couponNumber") String couponNumber) {
        try {
            tradeLogService.setLogInfo("COUPON", "QUERY_ONE_COUPON", "");
            tradeLogService.info(log, "CouponController queryOneCoupon,couponNumber:{},userId:{} ", couponNumber, userId);
            return couponService.queryOneCoupon(couponNumber, userId);
        } finally {
            tradeLogService.removeAll();
        }
    }
    @GetMapping("/queryUserCoupons/{userId}")
    public R queryUserCoupons(@PathVariable("userId") Integer userId) {
        try {
            tradeLogService.setLogInfo("COUPON", "QUERY_USER_COUPONS", "");
            tradeLogService.info(log, "CouponController queryUserCoupons,userId:{} ", userId);
            return couponService.queryUserCoupons(userId);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/queryUserCouponsByTargetSECity/{userId}/{startCity}/{endCity}")
    public R queryUserCouponsByTargetSECity(@PathVariable("userId") Integer userId, @PathVariable("startCity") String startCity, @PathVariable("endCity") String endCity) {
        try {
            tradeLogService.setLogInfo("COUPON", "QUERY_USER_COUPONS", "");
            tradeLogService.info(log, "CouponController queryUserCoupons,userId:{} ", userId);
            return couponService.queryUserCouponsByTargetSECity(userId, startCity, endCity);
        } finally {
            tradeLogService.removeAll();
        }
    }
}
